package com.by.controller.sys;

import com.by.entity.sys.DeptBean;
import com.by.common.bean.ResultBean;
import com.by.common.exception.ValidateException;
import com.by.controller.BaseController;
import com.by.service.sys.DeptService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/dept")
public class DeptController extends BaseController {

    @Autowired
    DeptService deptService;

    @GetMapping("/child")
    public ResultBean queryChild(String pid) {
        List list = deptService.query(pid);
        return success("部门列表", list);
    }

    @GetMapping("/childs")
    public ResultBean queryChildByCode(String code) {
        List list = deptService.queryChildByCode(code);
        return success("部门列表", list);
    }

    @GetMapping("/allchild")
    public ResultBean queryAllChild(String pid) throws JsonProcessingException {
        List list= deptService.queryAllChild(pid);
        return success("部门树", list);
    }

    @PostMapping("/save")
    public ResultBean save(@RequestBody @Validated DeptBean deptBean, BindingResult result) throws ValidateException {
        autoValidate(result);
        int rs = deptService.save(deptBean);
        return rs == 1 ? success("保存成功") : fail("保存失败");
    }


}
